<?php
	namespace Qwadmin\Controller;
	use Think\Controller;
	class VendorController extends ComController{
		public $db;
		public function __construct(){
			parent::__construct();
			$this->db=M('Vendors');
		}
		public function page(){
			$page=(int)$_GET['page'];
			if($page<=0){
				$page=1;
			}
			$info=$this->db->select();
			$list=array_slice($info, 10*($page-1),10);
			$this->ajaxReturn($list);

		}
		public function index(){
			$info=$this->db->select();
			$count=count($info);
			$Page= new \Think\Page($count,25);
		//$Page->setConfig('header',"条弹幕");
        	if($_GET['p']<1){
            	$_GET['p']=1;
        	}else{
            	$_GET['p']=(int)$_GET['p'];//
        	}
        	$list=array_slice($info, 25*($_GET['p']-1),25);
        	$show=$Page->show();
        	$this->assign('page',$show);
			$this->assign("info",$list);
			$this->display();
		}
		public function add_vendor(){
			//$post['vendor_id']=I('post.vendor_id');
			$post['vendor_code']=I('post.vendor_code');
			$jugg=$jugg=$this->is_valid_code($post['vendor_code']);
			if($jugg){//valid  false
				$re['status']=false;
				$re['con']="物料代码已经存在请重试！";
			}else{
				if(!($_POST['vendor_name']&&$_POST['vendor_address'])){
					$re['status']=false;
					$re['con']="料号和名称必须填入！";
					$this->ajaxReturn($re);
					return;
				}
				$post['vendor_name']=I('post.vendor_name');
				$post['vendor_address']=I('post.vendor_address');
				$post['vendor_contacts']=I('post.vendor_contacts');
				$post['contacts_phone']=I('post.contacts_phone');
				$post['contacts_mail']=I('post.contacts_mail');
				$post['created_by']=I('post.created_by');
				$post['last_updated_by']=I('post.last_updated_by');
				$post['CreditLimit']=I('post.CreditLimit');
				$post['currcode']=I('post.currcode');
				$post['typeid']=I('post.typeid');
				$post['position']=I('post.position');
				$post['effective_date']=I('post.effective_date');
				$post['disable_date']=I('post.disable_date');
				$post['creation_date']=I('post.creation_date');
				$post['last_update_date']=I('post.last_update_date');
				$post['currencycode']=I('post.currencycode');
				$post['bank_name']=I('post.bank_name');
				$post['bank_account']=I('post.bank_account');
				$post['created_by']=$this->uid();
				$post['creation_date']=time();
				$re['status']=$this->db->add($post);
			}
			$this->ajaxReturn($re);

		}
		public function delete_vendor(){
			$id=I('get.id');
			$this->del_vendor($id);
		}
		public function del_vendor($id){
			$jugg=$this->is_valid_id($id);
			if($jugg){
				$where['vendor_id']=$id;
				$re['status']=$this->db->where($where)->delete();
			}else{
				//不存在 返回不存在
				$re['status']=false;
				$re['con']="不存在该料号";
			}
			$this->ajaxReturn($re);
		}
		public function change_vendor(){
			//var_dump($_POST);
			$post['vendor_id']=I('post.vendor_id');
			if($this->is_valid_id($post['vendor_id'])){
				if(!($_POST['vendor_name']&&$_POST['vendor_address'])){
					$re['status']=false;
					$re['con']="料号和名称必须填入！";
					$this->ajaxReturn($re);
					return;
				}
				$post['vendor_name']=I('post.vendor_name');
				$post['vendor_address']=I('post.vendor_address');
				$post['vendor_contacts']=I('post.vendor_contacts');
				$post['contacts_phone']=I('post.contacts_phone');
				$post['contacts_mail']=I('post.contacts_mail');
			//	$post['created_by']=I('post.created_by');
			//	$post['last_updated_by']=I('post.last_updated_by');
			//	$post['CreditLimit']=I('post.CreditLimit');
				$post['currcode']=I('post.currcode');
			//	$post['typeid']=I('post.typeid');
			//	$post['position']=I('post.position');
			//	$post['effective_date']=I('post.effective_date');
			//	$post['disable_date']=I('post.disable_date');
			//	$post['creation_date']=I('post.creation_date');
			//	$post['last_update_date']=I('post.last_update_date');
				$post['currencycode']=I('post.currencycode');
			//	$post['bank_name']=I('post.bank_name');
			//	$post['bank_account']=I('post.bank_account');
				//var_dump($post);
				$post['last_updated_by']=$this->uid();
				$post['last_update_date']=time();
			 	$re['status']=$this->db->where('vendor_id='.$post['vendor_id'])->save($post);
				//echo $this->db->getLastSql();
			}else{
				$re['status']=false;
				$re['con']="不存在该信息！";
			}
			$this->ajaxReturn($re);
		}
		public function vendor_search(){//交互
			$search=I('get.name');
			$code=I('get.code');
			$this->search_name($search,$code);
		}
		public function vendor_id(){//交互
			$search=I('get.id');
			$this->search_id($search);
		}
		public function search_name($name,$code){//AJAX返回才才查找的信息
			$where['vendor_name']=array('like','%'.$name.'%');
			$where['vendor_code']=array('like','%'.$code.'%');
			$info=$this->db->where($where)->select();
			$this->ajaxReturn($info);
		}
		public function search_id($id){//AJAX返回才才查找的信息
			$where['vendor_id']=$id;
			$info=$this->db->where($where)->select();
			$this->ajaxReturn($info);
		}
		public function get_code_jugg(){
			$code=I('get.code');
			if($this->is_valid_code($code)){
				$re['status']=false;
			}else{
				$re['status']=true;
			}
			$this->ajaxReturn($re);//status=false的时候表示已经重复
		}
		public function is_valid_id($id){
			$where['vendor_id']=$id;
			$jugg=$this->db->where($where)->select();
			if($jugg){
				return true;
			}else{
				return false;
			}
		}
		public function is_valid_code($code){//匹配料号
			$where['vendor_code']=$code;
			$jugg=$this->db->where($where)->select();
			if($jugg){
				return true;
			}else{
				return false;
			}
		}
		public function uid(){
			$uid=$_SESSION['uid'];
			$uid=(int)$uid;
			$info=M('Member')->find($uid);
			return $info['user'];
		}

	}